from django.views.generic.base import View
from goods.models import Goods

class GoodsListView(View):
  def get(self, request):
    goods = Goods.objects.all()[:10]
    json_list = []
    # for good in goods:
    #   json_dict = {}
    #   json_dict['name'] = good.name
    #   json_dict['category'] = good.category.name
    #   json_list.append(json_dict)
    from django.forms.models import model_to_dict
    for good in goods:
      json_dict = model_to_dict(good)
      json_list.append(json_dict)
    print('================================')
    print(json_list)
    from django.core import serializers
    from django.http import JsonResponse
    import json
    json_data = serializers.serialize('json', goods)
    json_data = json.loads(json_data)

    return JsonResponse(json_data, safe=False)